﻿using DataImporter.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataImporter.Modules
{
    public partial class ucGJA : UserControl
    {
        private List<CC> lst = new List<CC>();
        private List<CCRisk> cCRisks = new List<CCRisk>();

        public ucGJA()
        {
            InitializeComponent();
            this.cbbDropdown.SelectedIndex = 0;
        }

        private DBContext DBContext => new DBContext();

        private void ofdFile_FileOk(object sender, CancelEventArgs e)
        {
            var table = this.ofdFile.FileName.GetDataTable(this.cbbDropdown.SelectedItem.ToString());
            if (this.cbbDropdown.SelectedIndex == 0)
                HandleFirst(table);
            else
                HandleSecond(table);
        }

        private void HandleFirst(DataTable table)
        {
            var codeIndex = 0;
            var question = string.Empty;
            var order = 0;
            var id = 0;
            var ruleIndex = 0;
            var answerIndex = 0;
            var lishu = 0;
            for (int i = 0; i < table.Columns.Count; i++)
            {
                var name = table.Rows[0][i].ToString();
                if (name.Contains("编码"))
                {
                    ruleIndex = 0;
                    codeIndex = i;
                    lishu = 0;
                }
                else if (name.Contains("规则"))
                    ruleIndex = i;
                else if (name.Contains("隶属"))
                    lishu = i;
                else if (name.Contains("指数"))
                {
                    for (int j = 1; j < table.Rows.Count; j++)
                    {
                        var row = table.Rows[j];
                        var answer = row[answerIndex].ToString()?.Trim();
                        if (string.IsNullOrWhiteSpace(answer))
                            continue;
                        lst.Add(new CC
                        {
                            Id = id += 1,
                            Answer = answer,
                            Score = float.Parse(row[i].ToString()),
                            Rule = ruleIndex > 0 ? row[ruleIndex].ToString() : null,
                            Code = row[codeIndex].ToString(),
                            Order = order += 5,
                            Question = question,
                            ParentId = lishu > 0 ? lst.Find(c => c.Code == row[lishu].ToString()).Id : 0
                        });
                    }
                }
                else
                {
                    question = name;
                    answerIndex = i;
                }
            }

            this.dgvData.DataSource = lst;
        }

        private void HandleSecond(DataTable table)
        {
            var ageRow = table.Rows[0];
            var ageRiskRow = table.Rows[1];
            var average = table.Rows[2];
            cCRisks.Clear();
            for (int i = 2; i < table.Columns.Count; i++)
            {
                var age = ageRow[i].ToString();
                if (age.IsNullOrEmpty())
                    return;
                var ageRisk = float.Parse(ageRiskRow[i].ToString());
                var averageRisk = float.Parse(average[i].ToString());
                if (age.Split('~') is String[] ages && ages.Length > 1)
                {
                    var start = int.Parse(ages[0]);
                    var end = int.Parse(ages[1]);
                    while (start <= end)
                    {
                        cCRisks.Add(new CCRisk
                        {
                            Age = start,
                            AgeScore = ageRisk,
                            AverageScore = averageRisk
                        });
                        start++;
                    }
                }
                else
                {
                    var _age = (int)float.Parse(age);
                    cCRisks.Add(new CCRisk
                    {
                        Age = _age,

                        AgeScore = ageRisk,
                        AverageScore = averageRisk
                    });
                }
            }
            this.dgvData.DataSource = cCRisks;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            this.ofdFile.ShowDialog();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (!DBContext.Db.DbMaintenance.IsAnyTable(nameof(CC)))
            //DBContext.Db.CodeFirst.InitTables<CC>();
            if (this.cbbDropdown.SelectedIndex == 0)
                DBContext.CCDB.InsertRange(lst);
            else
                DBContext.CCRiskDB.InsertRange(cCRisks);
            MessageBox.Show("保存成功");
        }
    }
}
